/*
 * Copyright (c) 2022 Hong Jiahua
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include <stdio.h>
#include <string.h>

#include "jh_share_deque.h"
/************************************************************************/
/*                                                                      */
/************************************************************************/
int main(int argc, char* argv[])
{
    //定义共享双端队列对象
    jh_share_deque_t share_deque;
    #ifdef JH_SHARE_QUEUE_DYNAMIC_SPACE
    //内存空间申请
    //参数：共享双端队列对象, 队列数量3, 队列总长度64, 用户数据类型大小
    jh_share_deque_space_malloc(&share_deque, 3, 64, sizeof(int));
    #else
    //定义用于存放数据的数组空间
    //参数：用户数据类型int, 队列总长度64, 数组名testbuf, 队列数量3
    JH_SHARE_QUEUE_DATA_SPACE(int, 64, testbuf, 3);
    
    //共享双端队列初始化
    //参数：共享双端队列对象, 队列数量3, 数组名testbuf, 队列总长度64, 数组大小
    jh_share_deque_init(&share_deque, 3, testbuf, 64, sizeof(testbuf));
    #endif
    
    int val;
    
    //数据添加到双端队列队尾
    for(int i = 0; i < 10; i++) {
        val = i * 10 + 1;
        //添加到队列1
        jh_share_deque_push_tail(&share_deque, 1, &val);
        val = i * 10 + 2;
        //添加到队列2
        jh_share_deque_push_tail(&share_deque, 2, &val);
        val = i * 10 + 3;
        //添加到队列3
        jh_share_deque_push_tail(&share_deque, 3, &val);
    }
    
    //队列1队首数据出队
    printf("id 1 : ");
    for(int i = 0; i < 10; i++) {
        jh_share_deque_pop_head(&share_deque, 1, &val);
        printf("%d ", val);
    }
    printf("\n");
    
    //队列2队首数据出队
    printf("id 2 : ");
    for(int i = 0; i < 10; i++) {
        jh_share_deque_pop_head(&share_deque, 2, &val);
        printf("%d ", val);
    }
    printf("\n");
    
    //队列3队首数据出队
    printf("id 3 : ");
    for(int i = 0; i < 10; i++) {
        jh_share_deque_pop_head(&share_deque, 3, &val);
        printf("%d ", val);
    }
    printf("\n");
    
    /******************************************************/
    printf("\n");
    
    //数据添加到双端队列队首
    for(int i = 0; i < 10; i++) {
        val = i * 10 + 1;
        //添加到队列1
        jh_share_deque_push_head(&share_deque, 1, &val);
        val = i * 10 + 2;
        //添加到队列2
        jh_share_deque_push_head(&share_deque, 2, &val);
        val = i * 10 + 3;
        //添加到队列3
        jh_share_deque_push_head(&share_deque, 3, &val);
    }
    
    //队列1队尾数据出队
    printf("id 1 : ");
    for(int i = 0; i < 10; i++) {
        jh_share_deque_pop_tail(&share_deque, 1, &val);
        printf("%d ", val);
    }
    printf("\n");
    
    //队列2队尾数据出队
    printf("id 2 : ");
    for(int i = 0; i < 10; i++) {
        jh_share_deque_pop_tail(&share_deque, 2, &val);
        printf("%d ", val);
    }
    printf("\n");
    
    //队列3队尾数据出队
    printf("id 3 : ");
    for(int i = 0; i < 10; i++) {
        jh_share_deque_pop_tail(&share_deque, 3, &val);
        printf("%d ", val);
    }
    printf("\n");
    
    /******************************************************/
    printf("\n");
    
    //数据添加到双端队列队尾
    for(int i = 0; i < 64; i++) {
        val = i * 10 + 1;
        jh_share_deque_push_tail(&share_deque, 1, &val);
        val = i * 10 + 2;
        jh_share_deque_push_tail(&share_deque, 2, &val);
        val = i * 10 + 3;
        jh_share_deque_push_tail(&share_deque, 3, &val);
    }
    
    //查看队列的状态
    printf("share_deque %d %s empty\n", 1, jh_share_deque_is_empty(&share_deque, 1) ? "is" : "isn't");
    printf("share_deque %d %s empty\n", 2, jh_share_deque_is_empty(&share_deque, 2) ? "is" : "isn't");
    printf("share_deque %d %s empty\n", 3, jh_share_deque_is_empty(&share_deque, 3) ? "is" : "isn't");
    printf("share_deque %s full\n", jh_share_deque_is_full(&share_deque) ? "is" : "isn't");
    
    printf("share_deque %d count %d\n", 1, jh_share_deque_count(&share_deque, 1));
    printf("share_deque %d count %d\n", 2, jh_share_deque_count(&share_deque, 2));
    printf("share_deque %d count %d\n", 3, jh_share_deque_count(&share_deque, 3));
    
    //查看队列1队首的数据
    jh_share_deque_peek_head(&share_deque, 1, &val);
    printf("share_deque %d head %d\n", 1, val);
    //查看队列2队首的数据
    jh_share_deque_peek_head(&share_deque, 2, &val);
    printf("share_deque %d head %d\n", 2, val);
    //查看队列3队首的数据
    jh_share_deque_peek_head(&share_deque, 3, &val);
    printf("share_deque %d head %d\n", 3, val);
    
    //查看队列1队尾的数据
    jh_share_deque_peek_tail(&share_deque, 1, &val);
    printf("share_deque %d tail %d\n", 1, val);
    //查看队列2队尾的数据
    jh_share_deque_peek_tail(&share_deque, 2, &val);
    printf("share_deque %d tail %d\n", 2, val);
    //查看队列3队尾的数据
    jh_share_deque_peek_tail(&share_deque, 3, &val);
    printf("share_deque %d tail %d\n", 3, val);
    
    //可以查看当前所有数据，从队首到队尾的顺序遍历
    printf("id 1 : ");
    JH_SHARE_QUEUE_ITER(&share_deque, 1, &val) {
        printf("%d ", val);
    }
    printf("\n");
    
    printf("id 2 : ");
    JH_SHARE_QUEUE_ITER(&share_deque, 2, &val) {
        printf("%d ", val);
    }
    printf("\n");
    
    printf("id 3 : ");
    JH_SHARE_QUEUE_ITER(&share_deque, 3, &val) {
        printf("%d ", val);
    }
    printf("\n");
    
    //可以查看当前所有数据，从队尾到队首的顺序遍历
    printf("id 1 : ");
    JH_SHARE_QUEUE_ITER_REVERSE(&share_deque, 1, &val) {
        printf("%d ", val);
    }
    printf("\n");
    
    printf("id 2 : ");
    JH_SHARE_QUEUE_ITER_REVERSE(&share_deque, 2, &val) {
        printf("%d ", val);
    }
    printf("\n");
    
    printf("id 3 : ");
    JH_SHARE_QUEUE_ITER_REVERSE(&share_deque, 3, &val) {
        printf("%d ", val);
    }
    printf("\n");
    
    //获取双端队列指定位置的指定数量数据
    int buf[64] = {0};
    int len;
    len = jh_share_deque_get_data(&share_deque, 1, buf, 0, 10);
    printf("id 1 len %d: ", len);
    for(int i = 0; i < len; i++) {
        printf("%d ", buf[i]);
    }
    printf("\n");
    
    len = jh_share_deque_get_data(&share_deque, 2, buf, 0, 10);
    printf("id 2 len %d: ", len);
    for(int i = 0; i < len; i++) {
        printf("%d ", buf[i]);
    }
    printf("\n");
    
    len = jh_share_deque_get_data(&share_deque, 3, buf, 0, 10);
    printf("id 3 len %d: ", len);
    for(int i = 0; i < len; i++) {
        printf("%d ", buf[i]);
    }
    printf("\n");
    
    //清空队列1
    jh_share_deque_clear(&share_deque, 1);
    printf("share_deque %d clear\n", 1);
    //清空队列2
    jh_share_deque_clear(&share_deque, 2);
    printf("share_deque %d clear\n", 2);
    //清空队列3
    jh_share_deque_clear(&share_deque, 3);
    printf("share_deque %d clear\n", 3);
    
    //查看队列的状态
    printf("share_deque %d %s empty\n", 1, jh_share_deque_is_empty(&share_deque, 1) ? "is" : "isn't");
    printf("share_deque %d %s empty\n", 2, jh_share_deque_is_empty(&share_deque, 2) ? "is" : "isn't");
    printf("share_deque %d %s empty\n", 3, jh_share_deque_is_empty(&share_deque, 3) ? "is" : "isn't");
    printf("share_deque %s full\n", jh_share_deque_is_full(&share_deque) ? "is" : "isn't");
    
    printf("share_deque %d count %d\n", 1, jh_share_deque_count(&share_deque, 1));
    printf("share_deque %d count %d\n", 2, jh_share_deque_count(&share_deque, 2));
    printf("share_deque %d count %d\n", 3, jh_share_deque_count(&share_deque, 3));
    
    /******************************************************/
    printf("\n");
    
    #ifdef JH_SHARE_QUEUE_DYNAMIC_SPACE
    //内存空间释放
    jh_share_deque_space_free(&share_deque);
    #endif

    return 0;
}